package com.bestom.bluetoothlock.util;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.FrameLayout;

import androidx.core.view.ViewCompat;

import com.bestom.bluetoothlock.R;

public class StatusBarCompat {
    private static final int COLOR_TRANSLUCENT = Color.parseColor("#00000000");
    public static final int DEFAULT_COLOR_ALPHA = 112;

    public static void setStatusBarColor(Activity activity, int i, int i2) {
        setStatusBarColor(activity, calculateStatusBarColor(i, i2));
    }

    public static void setStatusBarColor(Activity activity, int i) {
        Window window = activity.getWindow();
        ViewGroup viewGroup = (ViewGroup) activity.findViewById(16908290);
        if (Build.VERSION.SDK_INT >= 19) {
            window.addFlags(67108864);
            if (Build.VERSION.SDK_INT >= 21) {
                window.clearFlags(67108864);
                window.addFlags(Integer.MIN_VALUE);
                window.setStatusBarColor(i);
                View childAt = viewGroup.getChildAt(0);
                if (childAt != null) {
                    ViewCompat.setFitsSystemWindows(childAt, true);
                    return;
                }
                return;
            }
            ViewGroup viewGroup2 = (ViewGroup) window.getDecorView();
            if (viewGroup2.getTag() == null || !(viewGroup2.getTag() instanceof Boolean) || !((Boolean) viewGroup2.getTag()).booleanValue()) {
                int statusBarHeight = getStatusBarHeight(activity);
                View childAt2 = viewGroup.getChildAt(0);
                if (childAt2 != null) {
                    ViewCompat.setFitsSystemWindows(childAt2, false);
                    FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) childAt2.getLayoutParams();
                    layoutParams.topMargin += statusBarHeight;
                    childAt2.setLayoutParams(layoutParams);
                }
                View view = new View(activity);
                FrameLayout.LayoutParams layoutParams2 = new FrameLayout.LayoutParams(-1, statusBarHeight);
                layoutParams2.gravity = 48;
                view.setLayoutParams(layoutParams2);
                view.setBackgroundColor(i);
                viewGroup2.addView(view, 0);
                viewGroup2.setTag(true);
                return;
            }
            View childAt3 = viewGroup2.getChildAt(0);
            if (childAt3 != null) {
                childAt3.setBackgroundColor(i);
            }
        }
    }

    public static void translucentStatusBar(Activity activity) {
        translucentStatusBar(activity, false);
    }

    public static void translucentStatusBar(Activity activity, boolean z) {
        FrameLayout.LayoutParams layoutParams;
        Window window = activity.getWindow();
        ViewGroup viewGroup = (ViewGroup) activity.findViewById(16908290);
        View childAt = viewGroup.getChildAt(0);
        if (childAt != null) {
            ViewCompat.setFitsSystemWindows(childAt, false);
        }
        if (Build.VERSION.SDK_INT >= 19) {
            int statusBarHeight = getStatusBarHeight(activity);
            window.addFlags(67108864);
            if (Build.VERSION.SDK_INT >= 21) {
                window.addFlags(Integer.MIN_VALUE);
                if (z) {
                    window.clearFlags(67108864);
                    window.setStatusBarColor(COLOR_TRANSLUCENT);
                } else {
                    window.setStatusBarColor(calculateStatusBarColor(COLOR_TRANSLUCENT, 112));
                }
                if (childAt != null) {
                    ViewCompat.requestApplyInsets(childAt);
                    return;
                }
                return;
            }
            ViewGroup viewGroup2 = (ViewGroup) window.getDecorView();
            if (viewGroup2.getTag() != null && (viewGroup2.getTag() instanceof Boolean) && ((Boolean) viewGroup2.getTag()).booleanValue()) {
                viewGroup.removeView(viewGroup2.getChildAt(0));
                View childAt2 = viewGroup.getChildAt(0);
                if (!(childAt2 == null || (layoutParams = (FrameLayout.LayoutParams) childAt2.getLayoutParams()) == null || layoutParams.topMargin < statusBarHeight)) {
                    layoutParams.topMargin -= statusBarHeight;
                    childAt2.setLayoutParams(layoutParams);
                }
                viewGroup2.setTag(false);
            }
        }
    }

    @TargetApi(19)
    public static void transparencyBar(Activity activity) {
        if (Build.VERSION.SDK_INT >= 21) {
            Window window = activity.getWindow();
            window.clearFlags(67108864);
            window.getDecorView().setSystemUiVisibility(1280);
            window.addFlags(Integer.MIN_VALUE);
            window.setStatusBarColor(0);
        } else if (Build.VERSION.SDK_INT >= 19) {
            activity.getWindow().setFlags(67108864, 67108864);
        }
    }

    public static int getStatusBarHeight(Context context) {
        int identifier = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (identifier > 0) {
            return context.getResources().getDimensionPixelOffset(identifier);
        }
        return 0;
    }

    private static int calculateStatusBarColor(int i, int i2) {
        float f = 1.0f - (((float) i2) / 255.0f);
        double d = (double) (((float) ((i >> 16) & 255)) * f);
        Double.isNaN(d);
        int i3 = (int) (d + 0.5d);
        double d2 = (double) (((float) ((i >> 8) & 255)) * f);
        Double.isNaN(d2);
        double d3 = (double) (((float) (i & 255)) * f);
        Double.isNaN(d3);
        return ((int) (d3 + 0.5d)) | (i3 << 16) | ViewCompat.MEASURED_STATE_MASK | (((int) (d2 + 0.5d)) << 8);
    }
}
